package com.baidu.st.fe.spllot.ui;

import java.io.File;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.baidu.st.fe.spllot.util.Constant;
import com.baidu.st.fe.spllot.util.FileUtil;
import org.eclipse.swt.custom.StyledText;

public class CDMain {

	protected Shell shell;
	private Text filePath;
	private Text codeText;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CDMain window = new CDMain();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.MIN|SWT.CLOSE);
		shell.setSize(726, 714);
		shell.setText("\u4EE3\u7801\u514B\u9686\u68C0\u6D4B");
		shell.setLayout(null);
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("\u83DC\u5355");
		
		Menu menu_1 = new Menu(menuItem);
		menuItem.setMenu(menu_1);
		
		MenuItem new_menuItem = new MenuItem(menu_1, SWT.NONE);
		new_menuItem.setText("\u65B0\u5EFA");
		
		MenuItem export_menuItem = new MenuItem(menu_1, SWT.NONE);
		export_menuItem.setText("\u5BFC\u51FA");
		
		MenuItem chooseItem = new MenuItem(menu, SWT.CASCADE);
		chooseItem.setText("\u9009\u9879");
		
		Menu menu_2 = new Menu(chooseItem);
		chooseItem.setMenu(menu_2);
		
		MenuItem toolItem = new MenuItem(menu, SWT.CASCADE);
		toolItem.setText("\u5DE5\u5177");
		
		Menu menu_3 = new Menu(toolItem);
		toolItem.setMenu(menu_3);
		
		MenuItem mntmNewItem = new MenuItem(menu_3, SWT.NONE);
		mntmNewItem.setText("New Item");
		
		MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
		helpItem.setText("\u5E2E\u52A9");
		
		Menu menu_4 = new Menu(helpItem);
		helpItem.setMenu(menu_4);
		
		MenuItem software_helpItem = new MenuItem(menu_4, SWT.NONE);
		software_helpItem.setText("\u5173\u4E8E\u8F6F\u4EF6");
		
		MenuItem author_helpItem = new MenuItem(menu_4, SWT.NONE);
		author_helpItem.setText("\u5173\u4E8E\u4F5C\u8005");
		
		TabFolder tapFolder = new TabFolder(shell, SWT.NONE);
		tapFolder.setBounds(0, 0, 710, 623);
		
		TabItem sourceCodeTab = new TabItem(tapFolder, SWT.NONE);
		sourceCodeTab.setText("\u67E5\u770B\u6E90\u4EE3\u7801");
		
		Composite composite = new Composite(tapFolder, SWT.NONE);
		sourceCodeTab.setControl(composite);
		composite.setLayout(null);
		
		Group group = new Group(composite, SWT.NONE);
		group.setBounds(10, 10, 682, 66);
		
		Label label = new Label(group, SWT.NONE);
		label.setBounds(10, 29, 61, 17);
		label.setText("\u6587\u4EF6\u8DEF\u5F84");
		
		filePath = new Text(group, SWT.BORDER);
		filePath.setBounds(77, 26, 451, 23);
		
		Button viewButton = new Button(group, SWT.NONE);
		viewButton.setBounds(534, 24, 68, 27);
		viewButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				FileDialog fileDialog = new FileDialog(shell,SWT.OPEN);
				fileDialog.setFilterNames(Constant.ALLOW_FILE_NAME);
				fileDialog.setFilterExtensions(Constant.ALLOW_FILE_CATAGOLY);				
				String fileName = fileDialog.open();
				filePath.setText(fileName);
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		viewButton.setText("\u6D4F\u89C8...");
		
		Button loadButton = new Button(group, SWT.NONE);
		loadButton.setBounds(608, 24, 64, 27);
		loadButton.setText("\u8F7D\u5165");
		loadButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			    String fiPath = filePath.getText();
			    if(fiPath.trim().equals("")){
			    	MessageBox mb = new MessageBox(shell,SWT.OK);
			    	mb.setText(Constant.TITLE_TIP);
			    	mb.setMessage(Constant.TIP_NOFILE);
			    	mb.open();
			    	
			    }
			    else{
			    	File file = new File(fiPath);
			    	if(!file.isFile()){
			    		MessageBox mbBox = new MessageBox(shell,SWT.OK);
			    		mbBox.setText(Constant.TITLE_TIP);
			    		mbBox.setMessage(Constant.TIP_ISNOTFILE);
			    		mbBox.open();
			    	}
			    	else{
			    		String code="";
						try {
							code = FileUtil.readFile(fiPath);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			    		codeText.setText(code);
			    	}
			    }
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		Group group_1 = new Group(composite, SWT.NONE);
		group_1.setBounds(10, 82, 682, 501);
		
		codeText = new Text(group_1, SWT.BORDER|SWT.WRAP|SWT.V_SCROLL|SWT.H_SCROLL);
		codeText.setBounds(0, 0, 682, 501);
		
		TabItem projectItem = new TabItem(tapFolder, SWT.NONE);
		projectItem.setText("\u67E5\u770B\u5DE5\u7A0B\u6587\u4EF6");
		
		StyledText styledText = new StyledText(tapFolder, SWT.BORDER);
		styledText.setText("sgdgsdfgfd\r\ndhfdhfd\r\ndfhfdhfdh");
		projectItem.setControl(styledText);
		
		Label lblCopy = new Label(shell, SWT.NONE);
		lblCopy.setBounds(277, 639, 83, 17);
		lblCopy.setText("\u00A92011 Baidu");

	}
}
